﻿# Uncomment below and modify for testing
# $tfsDropFolder = "\\marknic-w500-01\BuildDrops\Test Project\MarkNicTestLib - NuGet - Versioning\MarkNicTestLib - NuGet - Versioning_20110621.7"
# $tfsSourcesFolder = "C:\Builds\4\Test Project\MarkNicTestLib - NuGet - Versioning\Sources"
# $tfsBinariesFolder = "C:\Builds\4\Test Project\MarkNicTestLib - NuGet - Versioning\Binaries"
# $tfsNuGetPrePackageFolder = "NuGetPrePackage"

# Calculate where the files will be copied for the NuGet Packaging process
if ([IO.Path]::IsPathRooted($nuGetPackageSourceFolder))
{
	$nugetPrePackageFolder = $tfsNuGetPrePackageFolder
}
else
{
	$nugetPrePackageFolder = Join-Path $tfsDropFolder $tfsNuGetPrePackageFolder
}

# Create some variables that will be used to create the package structure
$libFolder = "lib"
$contentFolder = "content" 
$toolsFolder = "tools" 

$net40 = "net40"
$net35 = "net35"

# Function to create a subfolder with some error checking and validation
Function Create-FrameworkFolder
{
	Param([string]$rootPath = $(throw "$rootPath required."), [string]$subFolder)

   	if ([System.String]::IsNullOrEmpty($subFolder))
	{
		$folderToCreate = $rootPath
	}
	else 
	{
		$folderToCreate = Join-Path $rootPath $subFolder
	}
	
    if (![IO.Directory]::Exists($folderToCreate)) 
	{
    	New-Item $folderToCreate -ItemType directory
	}
}

# Structure to Create:
# NuGetPrePackage
#                 \ content
#                           \ models
#                 \ lib
#                           \ net35
#                           \ net40
#                 \ tools


Create-FrameworkFolder -rootPath $nugetPrePackageFolder
Create-FrameworkFolder -rootPath $nugetPrePackageFolder -subFolder $contentFolder
Create-FrameworkFolder -rootPath $nugetPrePackageFolder -subFolder $libFolder
Create-FrameworkFolder -rootPath $nugetPrePackageFolder -subFolder $toolsFolder

$prePackageLibFolder = Join-Path $nugetPrePackageFolder $libFolder
Create-FrameworkFolder -rootPath $prePackageLibFolder -subFolder $net35
Create-FrameworkFolder -rootPath $prePackageLibFolder -subFolder $net40

# Identify the source location(s) for the files that were built as part of the TFS Build Process
$net35Folder = Join-Path $tfsDropFolder "MarkNicNuGetLib35"
$net40Folder = Join-Path $tfsDropFolder "MarkNicNuGetLib40"

# Copy all the files into position so NuGet can do the packaging
$dest = Join-Path $prePackageLibFolder $net35
Copy-Item "$net35Folder\*.dll" -Destination $dest

$dest = Join-Path $prePackageLibFolder $net40
Copy-Item "$net40Folder\*.dll" -Destination $dest

$dest = Join-Path $nugetPrePackageFolder $toolsFolder
Copy-Item "$net40Folder\tools\*.ps1" -Destination $dest

$dest = Join-Path $nugetPrePackageFolder $contentFolder
Copy-Item "$net40Folder\content\models" -Destination $dest -recurse
